﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LDSP.BRL;
using System.Data;
using System.IO;

namespace LDSP.UI.EnterpriseAdmin
{
    public partial class AddFavorateCodification : BRL.BaseAdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    this.CheckQS();
                }
                if (this.RB())
                {
                    if (!IsPostBack)
                    {
                        this.InitDataSelect();
                    }
                    this.InitDataDynamicAfter();
                }
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }

        protected Codification m_codification = null;
        protected Catalog m_catalog = null;

        protected int CatalogID
        {
            get { return this.GetVSInt("CatalogID"); }
            set
            {
                this.SetVS("CatalogID", value);
                m_catalog = null;
            }
        }
        protected int LastID
        {
            get { return this.GetVSInt("LastID"); }
            set
            {
                this.SetVS("LastID", value);
                m_codification = null;
            }
        }
        protected Codification Codification
        {
            get
            {
                if (m_codification == null)
                {
                    m_codification = new Codification();
                    if (LastID != 0)
                    {
                        m_codification.GetProperties(LastID);
                    }
                }
                return m_codification;
            }
        }
        protected Catalog Catalog
        {
            get
            {
                if (m_catalog == null)
                {
                    m_catalog = new Catalog();
                    if (CatalogID != 0)
                    {
                        m_catalog.GetProperties(CatalogID);
                    }
                }
                return m_catalog;
            }
        }
        protected bool IsAdd
        {
            get { return LastID == 0; }
        }

        protected void CheckQS()
        {
            CatalogID = this.GetInt(this.Request.QueryString["cid"]);
            if (Catalog.ID == 0)
            {
                CatalogID = 0;
            }
            LastID = this.GetInt(this.Request.QueryString["id"]);
            if (Codification.ID == 0)
            {
                LastID = 0;
            }
        }
        protected bool RB()
        {
            if (LastID == 0 && CatalogID == 0)
            {
                this.JSAlert("请选择栏目后添加！");
                this.JSCloseWindow(true);
                return false;
            }
            return true;
        }
        protected void SetStatus()
        {
            this.lbl_op.Text = IsAdd ? "【添加】" : "【修改】";
            this.lbl_op.Style["color"] = IsAdd ? "red" : "blue";
            this.ClientComfirm(this.btn_ok, string.Format("您确定执行{0}操作吗？", IsAdd ? "添加" : "修改"));
        }
        protected void InitDataSelect()
        {
            this.txt_name.Text = Codification.Name;
            this.txt_comment.Text = Codification.Comment;
            this.lbl_catalog.Text = LastID == 0 ? Catalog.Name : Codification.Catalog.Name;
            this.txt_organization.Text = Codification.Organization;
            this.txt_referenceNumber.Text = Codification.ReferenceNumber;
            this.txt_implementDT.Text = Codification.ImplementDT.ToString("yyyy-MM-dd");
            this.txt_releaseDT.Text = Codification.ReleaseDT.ToString("yyyy-MM-dd");
            this.chb_isValid.Checked = Codification.IsValid;
            
            this.BindDDLCategory();
            this.BindCHBInvalid();
            this.SetStatus();
        }
        protected void InitDataDynamicAfter()
        {
            this.JSSetDateTimeReadOnly(this.txt_implementDT.ClientID);
            this.JSSetDateTimeReadOnly(this.txt_releaseDT.ClientID);
        }

        protected void BindDDLCategory()
        {
            DataTable dt = new Category().GetAll();
            this.ddl_category.Items.Clear();
            this.ddl_category.Items.Add(new ListItem("请选择类别", "0"));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ddl_category.Items.Add(new ListItem(dt.Rows[i]["name"].ToString(), dt.Rows[i]["id"].ToString()));
            }
            try
            {
                this.ddl_category.SelectedIndex = 0;
                this.ddl_category.SelectedValue = Codification.CategoryID.ToString();
            }
            catch { }
        }
        protected void BindCHBInvalid()
        {
            DataTable dt = new Codification().GetRecordsByEnterpriseID(0);
            List<R_DisableDoc> invalidList = new R_DisableDoc().GetListBySource(LastID);
            this.chb_invalidList.Items.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Codification ett = new Codification();
                ett.GetProperties(dt.Rows[i]);
                if (ett.ID == LastID)
                {
                    continue;
                }
                ListItem li = new ListItem(ett.Name, ett.ID.ToString());
                chb_invalidList.Items.Add(li);
                for (int j = 0; j < invalidList.Count; j++)
                {
                    if (ett.ID == invalidList[i].DestinationID)
                    {
                        li.Selected = true;
                        break;
                    }
                }
            }
        }

        protected void CreateDisableRelationship(int sourceID)
        {
            for (int i = 0; i < chb_invalidList.Items.Count; i++)
            {
                if (chb_invalidList.Items[i].Selected)
                {
                    R_DisableDoc ett = new R_DisableDoc();
                    ett.SourceID = sourceID;
                    ett.DestinationID = this.GetInt(chb_invalidList.Items[i].Value);
                    ett.Add();
                }
            }
        }

        protected void ValidateDataBefore()
        {
        }
        protected void ValidateDataAfter()
        {
            if (Codification.Name == "")
            {
                throw new BYFW.BOL.CustomException("名称不能为空！");
            }
        }
        protected void SetData()
        {
            this.ValidateDataBefore();
            Codification.Name = this.txt_name.Text.Trim();
            Codification.Comment = this.txt_comment.Text;
            Codification.ReleaseDT = DateTime.Parse(this.txt_releaseDT.Text.Trim());
            Codification.ImplementDT = DateTime.Parse(this.txt_implementDT.Text.Trim());
            Codification.CategoryID = this.GetInt(this.ddl_category.SelectedValue);
            Codification.Organization = this.txt_organization.Text.Trim();
            Codification.ReferenceNumber = this.txt_referenceNumber.Text.Trim();
            Codification.IsValid = this.chb_isValid.Checked;
            if(IsAdd)
            {
                Codification.CatalogID = CatalogID;
                Codification.FileExtension = Guid.NewGuid().ToString();
            }
            else
            {
                new R_DisableDoc().DeleteBySource(Codification.ID);
            }
            if (this.fup_file.HasFile)
            {
                Codification.FileName = this.fup_file.FileName;
                this.fup_file.SaveAs(Codification.GetDownloadFileFullName());
            }
            
            this.ValidateDataAfter();
        }
        protected void Add()
        {
            this.SetData();
            Codification.Add();
            this.LastID = 0;
            this.JSAlert("添加成功！");
        }
        protected void Update()
        {
            string tmp = Codification.Name;
            this.SetData();
            if (tmp != Codification.Name)
            {
                new DownloadLog().UpdateByCodification(Codification.ID, Codification.Name);
            }
            Codification.Update();
            this.JSAlert("更新成功！");
        }

        protected void btn_add_Click(object sender, EventArgs e)
        {
            try
            {
                this.LastID = 0;
                this.InitDataSelect();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void btn_ok_Click(object sender, EventArgs e)
        {
            try
            {
                if (IsAdd)
                {
                    this.Add();
                }
                else
                {
                    this.Update();
                }
                this.InitDataSelect();

            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }

    }

}